package com.syezon.wifikey.ad;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.hodanet.appadvplatclient.utils.ClientInfoUtils;
import com.syezon.wifikey.api.HttpClient;
import com.syezon.wifikey.channel.ChannelConfig;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SDKReportHelper {

    private static final String TAG = SDKReportHelper.class.getSimpleName();

    private static ExecutorService executorService = Executors.newCachedThreadPool();

    private static final String URL = "http://stwifi.51woshow.com/doc/reportSDKAdv.htm";

    public static final int SOURCE_GDT = 1;
    public static final int SOURCE_CSJ = 2;

    public static final int OP_TYPE_SHOW = 1;
    public static final int OP_TYPE_CLICK = 2;
    public static final int OP_TYPE_DOWNLOAD = 3;
    public static final int OP_TYPE_INSTALL = 4;
    public static final int OP_TYPE_ACTIVE = 5;
    public static final int OP_TYPE_FAIL = 6;


    public static void reportSDKAdv(int source, String advId, int opType) {
        report(source, advId, opType);
    }

    private static void report(final int source, final String advId, final int opType) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    LogUtils.d(TAG, "source : " + source + " advId : " + advId + " opType : " + opType);
                    Map<String, Object> paramMap = new LinkedHashMap<>();
                    paramMap.put("appId", AdConfig.APP_ID);
                    paramMap.put("channel", ChannelConfig.getChannel() + "");
                    paramMap.put("ver", AppUtils.getAppVersionCode() + "");
                    paramMap.put("versionCode", AppUtils.getAppVersionCode() + "");
                    paramMap.put("versionName", AppUtils.getAppVersionName() + "");
                    paramMap.put("imei", ClientInfoUtils.getImei() + "");

                    paramMap.put("source", source);
                    paramMap.put("advId", advId);
                    paramMap.put("opType", opType);

                    StringBuilder sb = new StringBuilder();
                    Iterator<Map.Entry<String, Object>> iterator = paramMap.entrySet().iterator();
                    int size = paramMap.size();
                    int index = 0;
                    sb.append(URL)
                            .append("?");
                    while (iterator.hasNext()) {
                        Map.Entry<String, Object> next = iterator.next();
                        sb.append(next.getKey()).append("=").append(next.getValue());
                        index++;
                        if (index < size) {
                            sb.append("&");
                        }
                    }
                    String result = HttpClient.getResult(sb.toString(), "");
                    LogUtils.d(TAG, result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
